# -*- coding: utf-8 -*-
# @Time    : 2021/2/19 下午8:19
# @Author  : tangbing
# @Email   : tangbingb@chanjet.com
import hashlib
import json
import random

import requests

from Common import Log
from Conf import Config
from Common import Hash
from Common import Ramdom
from Common import Time
# from Common import Hash
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class Session:
    def __init__(self):
        self.config = Config.Config()
        self.log = Log.MyLog()
    #
    # def getSession(self):
    #     pass

    def getYdzAccess_token(self):
        requestURL = "http://cia.chanapp.chanjet.com/internal_api/client_authentication_with_userInfo"
        password = Hash.my_md5("1q2w3e")
        # md5 = hashlib.md5()
        # md5.update(password.encode('utf-8'))
        # print(md5.hexdigest())
        login_params = {
            # 易代账
            "client_id": "3cbfb2f3-e58a-4d10-821b-d6bb3a04376",
            "client_secret": "dlsdvz",
            "auth_username": "13466398443",
            "password": password
        }

        print(requestURL)
        print(login_params)
        req = requests.session()
        response = req.post(requestURL, verify=False, params=login_params)
        result = json.loads((response.content).decode('utf-8'))
        print(response.content)
        token = result["access_token"]
        return token

    # 动态获取code
    def getAuth_code(self):
        client_id = "4cb832be-e503-4075-9903-6aa8d9e29104"
        callback = "jQuery" + Ramdom.random_gudingLength(30)
        time = Time.timeMs()
        url = "https://inte-cia.chanapp.chanjet.com/internal_api/authorizeByJsonp?client_id=%s&callback=%s&jsonp=true&_=%s" % (
            client_id, callback, time)
        respones = requests.get(url)

        print(respones.content)
        auth_code = str(respones.content, 'utf-8')
        authorcode = auth_code[51:57]
        print(authorcode)
        return authorcode

    def weblogin(self):
        callback = "jQuery" + Ramdom.random_gudingLength(30)
        username = "15038283002"
        passwd = "801064"
        # 密码加密处理
        password = Hash.my_md5(passwd)
        auth_code = self.getAuth_code()
        # 动态获取时间戳
        time1 = Time.timeMs()

        url = "https://passport.inte.chanjet.com/loginV2/webLogin?callback=%s&auth_username=%s&password=%s&auth_code=%s&jsonp=1&_=%s" % (
            callback, username, password, auth_code, time1)
        # print(url)
        s = requests.session()
        s.keep_alive = False
        respones = s.get(url, verify=False)

        contetent = str(respones.content, 'utf-8')
        content = contetent.split(callback)[1]
        content = content.lstrip("(")
        content = content.rstrip(");'")

        res = json.loads(content)
        # print(res)
        return res

    # 动态获取token
    def getAccess_token(self):
        return self.weblogin()["access_token"]


    def getAuthorization(self):
        username = "15038283002"
        password = "801064"
        domain = "https://inte-cloud.chanjet.com/ydzee/up142n6n454b/3gukm5tz3y"
        url = "http://172.18.22.177:8000/login/auth?username=" + username + "&password=" + password + "&domain_host=" + domain + "&inner_app=wsc"

        respones = requests.get(url)
        authorization = str(respones.content, 'utf-8')
        print(authorization)

        return authorization


if __name__ == '__main__':
    ss = Session()
    # ss.getAuthorization()
    # ss.getUserInfo()
    # ss.getAuth_code()
    ss.weblogin()
    # ss.getAccess_token()
